import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:pd_application/routes.dart';
import 'package:pd_application/utils/http.dart';
import 'package:pd_application/utils/totast.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  var usernameController = TextEditingController();
  var passwordController = TextEditingController();
  var confPasswordController = TextEditingController();

  _registerHandler() async {
    // 处理注册逻辑
    // var username = usernameController.text;
    var password = passwordController.text;
    var confPassword = confPasswordController.text;
    if (password != confPassword) {
      ToastHelper.showToast(context, '两次输入的密码不一致');
      return;
    }
    const url = '/login/zhuce';
    var params = {
      'username': usernameController.text,
      'password': passwordController.text,
    };
    var response = await HttpApi.request(url, params: params);
    var responseData = json.decode(response.toString());

    if (responseData['code'] == 1) {
      // ignore: use_build_context_synchronously
      ToastHelper.showToast(context, '注册成功');
      // ignore: use_build_context_synchronously
      Navigator.pushReplacementNamed(context, Routes.LOGIN);
    } else {
      // ignore: use_build_context_synchronously
      ToastHelper.showToast(context, responseData['msg']);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('注册'),
        backgroundColor: Theme.of(context).primaryColor,
      ),
      body: SafeArea(
        minimum: const EdgeInsets.all(30),
        child: ListView(children: [
          // 用户名
          TextField(
              controller: usernameController,
              decoration: const InputDecoration(
                prefixIcon: Icon(Icons.person),
                labelText: '用户名',
                hintText: '请输入用户名',
              )),
          const Padding(padding: EdgeInsets.all(10.0)),
          // 密码
          TextField(
              controller: passwordController,
              obscureText: true,
              decoration: const InputDecoration(
                prefixIcon: Icon(Icons.lock),
                labelText: '密码',
                hintText: '请输入密码',
              )),
          const Padding(padding: EdgeInsets.all(10.0)),
          // 确认密码
          TextField(
              controller: confPasswordController,
              obscureText: true,
              decoration: const InputDecoration(
                prefixIcon: Icon(Icons.lock),
                labelText: '确认密码',
                hintText: '请输入确认密码',
              )),
          const Padding(padding: EdgeInsets.all(10.0)),
          // 添加其他表单元素
          // 登录按钮
          ElevatedButton(
              style: ElevatedButton.styleFrom(
                minimumSize: const Size.fromHeight(50), // 设置按钮高度
                backgroundColor: Theme.of(context).primaryColor,
              ),
              onPressed: _registerHandler,
              child: const Text(
                '注册',
                style: TextStyle(
                  fontSize: 20,
                  color: Colors.white,
                ),
              )),
          const Padding(padding: EdgeInsets.all(10.0)),
          // 注册按钮
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('已有账号？'),
              TextButton(
                  onPressed: () {
                    // 处理注册按钮点击事件
                    Navigator.pushReplacementNamed(context, Routes.LOGIN);
                  },
                  child: const Text(
                    '登录',
                    style: TextStyle(
                      color: Colors.blue,
                    ),
                  ))
            ],
          )
        ]),
      ),
    );
  }
}
